/**************************************************************************
 * @doc ISODump
 * @module ISODump.C |  
 * Source code for the ISODump handling functions.
 *
 * Product         :   SoftPay 2000 (Global Payment Methodology).
 * <nl>Developer   :   SoftPay Development Team (I_SP2000).
 * <nl>Notes       :
 *
 * @head3 ISODump Functions | 
 * @index | ISODump
 * @end
 *
 * Copyright (c) 1996-2000 by VeriFone Inc. All rights reserved.
 * 
 * No part of this software may be used, stored, compiled, reproduced,
 * modified, transcribed, translated, transmitted, or transferred, in any form 
 * or by any means whether electronic, mechanical, magnetic, optical, 
 * or otherwise, without the express prior written permission of VeriFone, Inc.
 *
 * Revision History
 * ------------------------------------------------------------------------
 $Log: /SoftPay/SPK2SIM/Source/ISODump/ISODump.c $
 ********************************************************************************/

#include <project.h>

#include <string.h>
#include <svc.h>

#include "..\Include\Define.h"
#include "..\Include\Print.h"
#include "..\..\HostSrc\Cpacsrc\Dbfm.h"
#include "..\Include\Date.h"

#include "ISODump.h"

extern PRINTER_OBJECT obPrinter;

char szISO8583MessageSet[][32] = 	{
									"\n\n",
									"NO RECORDS FOUND",
									"ERROR READING RECORD",
									"ERROR WRITING RECORD",
									"ERROR PRINTING RECORD",
									"ERROR DELETING RECORD",
									"ERROR CREATING FILE"
								};

/******************************************************************************
 * Routine Name:   inPrintISO8583PacketLog()
 * Description:
 * Input:			
 * Output:		VS_SUCCESS / Error code
 * Notes:			This is an experimental feature currently being implemented for debug purp-
 				oses in the field. This functions dumps the ISO8583 log to the printer.
 *******************************************************************************/

int inPrintISO8583PacketLog(void)
{
	int inStatus;
	int inLoopIndex;
	int inTotalRecords;

	unsigned char szFormattedISOPacket[ISODUMP_MAX_DATA_SIZE * 2];
	char szFormattedDateTime[ISODUMP_DATE_SIZE + ISODUMP_TIME_SIZE];

	ISODUMP_LOG_OBJECT obISO8583Log;

	inTotalRecords = inDBTotalRecords(ISODUMP_LOG_FILE_NAME);

	if(inTotalRecords < 0)
	{
		inPrintErrorMessage(ISODUMP_READFAIL_MSG);
		return ISODUMP_ERROR_READING_RECORD;
	}
	else if(inTotalRecords == 0)
		{
			inPrintErrorMessage(ISODUMP_NORECORDS_MSG);
			return VS_SUCCESS;
		}

	shSpool(&obPrinter, szISO8583MessageSet[ISODUMP_FORMFEED_MSG]);

	for(inLoopIndex = 1; inLoopIndex <= inTotalRecords; inLoopIndex++)
	{
		memset(&obISO8583Log, 0x00, ISODUMP_LOG_OBJECT_SIZE);
		inStatus = inDBReadRecord(ISODUMP_LOG_FILE_NAME, inLoopIndex, (unsigned char*)&obISO8583Log);

		if(inStatus <= 0)
		{
			inPrintErrorMessage(ISODUMP_READFAIL_MSG);
			return ISODUMP_ERROR_READING_RECORD;
		}

		memset(szFormattedDateTime, 0x00, sizeof(szFormattedDateTime));
		sprintf(szFormattedDateTime, "%s @ %s", (char*) obISO8583Log.szDate, (char*) obISO8583Log.szTime);

		memset(szFormattedISOPacket, 0x00, sizeof(szFormattedISOPacket));
		inFormatISO8583Packet(szFormattedISOPacket, obISO8583Log.szISO8583Data, inStatus);

		shSpool(&obPrinter, (char*) szFormattedDateTime);
		shSpool(&obPrinter, szISO8583MessageSet[ISODUMP_FORMFEED_MSG]);	
		
		shSpool(&obPrinter, (char*) szFormattedISOPacket);
		shSpool(&obPrinter, szISO8583MessageSet[ISODUMP_FORMFEED_MSG]);
	}

	shSpool(&obPrinter, szISO8583MessageSet[ISODUMP_FORMFEED_MSG]);
	shSpool(&obPrinter, szISO8583MessageSet[ISODUMP_FORMFEED_MSG]);
	shSpool(&obPrinter, szISO8583MessageSet[ISODUMP_FORMFEED_MSG]);
	
	return VS_SUCCESS;

}

/******************************************************************************
 * Routine Name:   inWriteISO8583PacketToLog()
 * Description:
 * Input:			unsigned char* szPacket, int inLength
 * Output:		VS_SUCCESS / Error code
 * Notes:			This function writes the ISO8583 packet to the packet log
 ****************************************************************************** */

int inWriteISO8583PacketToLog(unsigned char* szPacket, int inLength)
{
	int inStatus;
	int inWriteStatus;
	int inTotalRecords;
	int inRecordNumber;
		
	ISODUMP_LOG_OBJECT obISO8583Log;

	memset(&obISO8583Log, 0x00, ISODUMP_LOG_OBJECT_SIZE);

	vdGetDate((char*) obISO8583Log.szDate, YYMMDD);
	vdFormatDate((char*) obISO8583Log.szDate);

	vdGetTime((char*) obISO8583Log.szTime);
	vdFormatTime((char*) obISO8583Log.szTime);
	
	SVC_HEX_2_DSP((char*)szPacket, (char*)obISO8583Log.szISO8583Data, inLength);

	inStatus = inDBFileExists(ISODUMP_LOG_FILE_NAME);

	if(inStatus != VS_SUCCESS)
	{
		inStatus = inDBCreateFile(ISODUMP_LOG_FILE_NAME, ISODUMP_LOG_OBJECT_SIZE);

		if(inStatus != VS_SUCCESS)
		{
			inPrintErrorMessage(ISODUMP_CREATEFAIL_MSG);
			return ISODUMP_ERROR_CREATING_FILE;
		}
		
		inRecordNumber = 1;
		inWriteStatus = VS_SUCCESS;
	}
	else
	{
		inTotalRecords = inDBTotalRecords(ISODUMP_LOG_FILE_NAME);

		if(inTotalRecords <= 0)
		{
			inPrintErrorMessage(ISODUMP_READFAIL_MSG);
			return ISODUMP_ERROR_READING_RECORD;
		}

		if(inTotalRecords >= ISODUMP_MAX_RECORDS)
		{
			inStatus = inDBDeleteRecord(ISODUMP_LOG_FILE_NAME, 1);

			if(inStatus != VS_SUCCESS)
			{
				inPrintErrorMessage(ISODUMP_DELETEFAIL_MSG);
				return ISODUMP_ERROR_DELETING_RECORD;
			}
		}

		inRecordNumber = DB_APPENDREC;
		inWriteStatus = VS_SUCCESS;
	}

	if(inWriteStatus == VS_SUCCESS)
	{
		inStatus = inDBWriteRecord(ISODUMP_LOG_FILE_NAME, inRecordNumber, (const unsigned char*)&obISO8583Log);

		if(inStatus < 0)
		{
			inPrintErrorMessage(ISODUMP_WRITEFAIL_MSG);
			return ISODUMP_ERROR_WRITING_RECORD;
		}
	}

	return VS_SUCCESS;
}

/**********************************************************************************
 * Routine Name:   inFormatISO8583Packet()
 * Description:
 * Input:			unsigned char* szFormattedPacket, unsigned char* szOriginalPacket, int inLength
 * Output:		VS_SUCCESS
 * Notes:			This function formats the ISO8583 packet in VeriFind style
 **********************************************************************************/

int inFormatISO8583Packet(unsigned char* szFormattedPacket, unsigned char* szOriginalPacket, int inLength)
{
	int inOPIndex;
	int inFPIndex;

	for(inOPIndex = 0, inFPIndex = 0 ; inOPIndex < inLength; inOPIndex++, inFPIndex++)
	{
		szFormattedPacket[inFPIndex] = szOriginalPacket[inOPIndex];

		if(inOPIndex % 2)
		{
			inFPIndex++;
			szFormattedPacket[inFPIndex] = 0x20;
		}
	}
	
	return VS_SUCCESS;
}

/******************************************************************************
 * Routine Name:   inPrintErrorMessage()
 * Description:
 * Input:			int inMessageIndex
 * Output:		VS_SUCCESS
 * Notes:			This function prints an error message on the printer
 *******************************************************************************/

int inPrintErrorMessage(int inMessageIndex)
{
	char szErrorMessage[ISODUMP_ERROR_MSG_SIZE];

	memset(szErrorMessage, 0x00, ISODUMP_ERROR_MSG_SIZE);

	strcpy(szErrorMessage,	szISO8583MessageSet[ISODUMP_FORMFEED_MSG]);
	strcat(szErrorMessage, 	szISO8583MessageSet[inMessageIndex]);
	strcat(szErrorMessage, 	szISO8583MessageSet[ISODUMP_FORMFEED_MSG]);
	
	shSpool(&obPrinter, szErrorMessage);

	return VS_SUCCESS;
}

